<?php

namespace app\admin\controller;

use app\admin\common\Base;

/**
 * 员工控制器
 * Class Employee
 * @package app\admin\controller
 */
class Employee extends Base {
    public function index($page = 1, $limit = 10){
        if (is_post()) {
            $where[] = ['is_del', '=', 0];

            $data = $this->getListData('employee', $where, 'sort', 'asc', $page, $limit);

            return json(['code' => 'suc', 'msg' => '数据请求成功', 'count' => $data['count'], 'data' => $data['list'], 'current_page' => $page]);
        }
        return $this->fetch();
    }

    public function add(){
        if (is_post()){
            $data = $this->handleData(input('post.'),'add');
            $validate = $this->validateData($data);
            if ($validate['code'] == 'suc'){
                $result = db('employee')->insertGetId($data,true);
                if ($result){
                    insert_system_log('新增员工：' . $result);
                    return json(['code' => 'suc', 'msg' => config('msg.add_suc')]);
                }else {
                    return json(['code' => 'err', 'msg' => config('msg.err_msg')]);
                }
            }else{
                return json(['code' => $validate['code'], 'msg' => $validate['msg']]);
            }

        }
        $this->assign('position',$this->getPosition());
        return $this->fetch();
    }

    public function edit($id){
        if (is_post()){
            $data = $this->handleData(input('post.'),'edit');
            $validate = $this->validateData($data);

            if ($validate['code'] == 'suc'){
                $result = db('employee')->where('id',$id)->update($data);
                if ($result){
                    insert_system_log('删除员工：' . $id);
                    return json(['code' => 'suc', 'msg' => config('msg.edit_suc')]);
                }else {
                    return json(['code' => 'err', 'msg' => config('msg.err_msg')]);
                }
            }else{
                return json(['code' => $validate['code'], 'msg' => $validate['msg']]);
            }

        }
        $info = db('employee')->where('id',$id)->find();
        $this->assign('info',$info);
        $this->assign('position',$this->getPosition());
        return $this->fetch();
    }

    public function del($id){
        if (is_post()) {
            if ($this->delRecord('employee', $id, '删除员工：')) {
                return json(['code' => 'suc', 'msg' => config('msg.del_suc')]);
            }
            return json(['code' => 'err', 'msg' => config('msg.err_msg')]);
        }
    }

    /**
     * 处理数据
     * @param $data
     * @param $action
     * @return mixed
     */
    private function handleData($data,$action){
        if ($action == 'add'){
            $data['create_time'] = time();
        }

        $data['status'] = empty($data['status']) ? 0 : 1;
        $data['is_recommended'] = empty($data['is_recommended']) ? 0 : 1;
        $data['update_time'] = time();

        return $data;
    }

    /**
     * 验证数据
     * @param $data
     * @return array
     */
    private function validateData($data){
        $code = 'suc';
        $msg = '';
        if (empty($data['name'])){
            $code = 'err';
            $msg = '姓名不能为空';
        }

        return ['code' => $code,'msg' => $msg];
    }

    public function getPosition(){
        $where[] = ['status','=',1];
        $where[] = ['is_del','=',0];
        $list = db('position')
            ->where($where)
            ->order('sort','asc')
            ->field('id,name')
            ->select();
        return $list;
    }

    public function changeStatus($type, $id, $field){
        if (is_post()) {
            switch ($field) {
                case 1:
                    $log_action = $type==1 ? '显示' : '隐藏';
                    $action_str = $type==1 ? '已显示' : '已隐藏';
                    break;
                case 2:
                    $log_action = $type==1 ? '推荐' : '取消推荐';
                    $action_str = $type==1 ? '已推荐' : '取消推荐';
                    break;
                default:
                    $log_action = $type==1 ? '显示' : '隐藏';
                    $action_str = $type==1 ? '已显示' : '已隐藏';
            }

            if ($this->changeRecordStatus('employee',$id,$field,$type)) {
                insert_system_log($log_action . '员工：' . $id);
                return json(['code' => 'suc', 'msg' => $action_str . '员工']);
            }
            return json(['code' => 'err','msg' => config('msg.err_msg')]);
        }
    }

    public function upload(){
        if (is_post()) {
            $file = request()->file('file');
            $info = $this->uploadsImg($file, 'uploads/employee/');
            if ($info !== false) {
                return json(['code' => 'suc', 'msg' => '图片上传成功', 'url' => $info['path']]);
            } else {
                return json(['code' => 'err', 'msg' => '图片上传失败，请重试']);
            }
        }

    }
}